package cn.com.xf.service.impl;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.NumberToTextConverter;
import org.springframework.web.multipart.MultipartFile;

import cn.com.xf.domain.TblCheckData;
import cn.com.xf.service.DataImportService;
import cn.com.xf.service.DataService;
import cn.com.xf.utils.DateUtils;
import cn.com.xf.web.XFContainsTool;

/**
 * 长安分公司考勤数据导入
 *
 */
public class DataImportService769001Impl implements DataImportService {

	private DataService dataService = (DataService) XFContainsTool.lookup("dataService");
	
	@Override
	public void importCheckData(MultipartFile file,String siteid,  String entersite, String adduser) throws Exception {
		InputStream is = file.getInputStream();
		HSSFWorkbook workbook = new HSSFWorkbook(is);
		int n = 2;
		while(n < workbook.getNumberOfSheets()) {
			HSSFSheet sheet = workbook.getSheetAt(n);
			for (int i = 0; i < 3; i++) {
				String dateString = getCellValue(sheet.getRow(4).getCell(1 + i*15));
				String[] parts = dateString.split("～");
				String[] startdateParts = parts[0].trim().split("-");
				String[] enddateParts = parts[1].trim().split("-");
				int startDay = Integer.valueOf(startdateParts[2]);
				int endDay = Integer.valueOf(enddateParts[2]);
				String date = startdateParts[0] + "-" + startdateParts[1];
				String userid = getCellValue(sheet.getRow(4).getCell(9 + i*15));
				for (int j = 12; j <= 12 + endDay - startDay; j++) {
					String checkintime1 = getCellValue(sheet.getRow(j).getCell(1 + i*15));
					String checkouttime1 = getCellValue(sheet.getRow(j).getCell(3 + i*15));
					String checkintime2 = getCellValue(sheet.getRow(j).getCell(6 + i*15));
					String checkouttime2 = getCellValue(sheet.getRow(j).getCell(8 + i*15));
					if (!checkintime1.equals("")&&!checkintime1.equals("旷 工")) {
						int day = j - 11;
						String timedate = "";
						if (day < 10) {
							timedate = date + "-0" + day + " "; 
						} else {
							timedate = date + "-" + day + " "; 
						}
						Date checktime = DateUtils.parseDatetime(timedate + checkintime1);
						TblCheckData checkData = new TblCheckData();
						checkData.setSiteid("769001");
						checkData.setEntersite(entersite);
						checkData.setEnteruser(adduser);
						checkData.setEntertime(new Date());
						checkData.setUserid(userid);
						checkData.setChecktime(checktime);
						checkData.setType((short) 0);
						dataService.insertData(checkData);
					}
					if (!checkouttime1.equals("")&&!checkouttime1.equals("旷 工")) {
						int day = j - 11;
						String timedate = "";
						if (day < 10) {
							timedate = date + "-0" + day + " "; 
						} else {
							timedate = date + "-" + day + " "; 
						}
						Date checktime = DateUtils.parseDatetime(timedate + checkouttime1);
						TblCheckData checkData = new TblCheckData();
						checkData.setSiteid("769001");
						checkData.setEntersite(entersite);
						checkData.setEnteruser(adduser);
						checkData.setEntertime(new Date());
						checkData.setUserid(userid);
						checkData.setChecktime(checktime);
						checkData.setType((short) 1);
						dataService.insertData(checkData);
					}
					if (!checkintime2.equals("")&&!checkintime2.equals("旷 工")) {
						int day = j - 11;
						String timedate = "";
						if (day < 10) {
							timedate = date + "-0" + day + " "; 
						} else {
							timedate = date + "-" + day + " "; 
						}
						Date checktime = DateUtils.parseDatetime(timedate + checkintime2);
						TblCheckData checkData = new TblCheckData();
						checkData.setSiteid("769001");
						checkData.setEntersite(entersite);
						checkData.setEnteruser(adduser);
						checkData.setEntertime(new Date());
						checkData.setUserid(userid);
						checkData.setChecktime(checktime);
						checkData.setType((short) 0);
						dataService.insertData(checkData);
					}
					if (!checkouttime2.equals("")&&!checkouttime2.equals("旷 工")) {
						int day = j - 11;
						String timedate = "";
						if (day < 10) {
							timedate = date + "-0" + day + " "; 
						} else {
							timedate = date + "-" + day + " "; 
						}
						Date checktime = DateUtils.parseDatetime(timedate + checkouttime2);
						Date checkdate = DateUtils.parseDate(timedate);
						TblCheckData checkData = new TblCheckData();
						checkData.setSiteid("769001");
						checkData.setEntersite(entersite);
						checkData.setEnteruser(adduser);
						checkData.setEntertime(new Date());
						checkData.setUserid(userid);
						checkData.setChecktime(checktime);
						checkData.setCheckdate(checkdate);
						checkData.setType((short) 1);
						dataService.insertData(checkData);
					}
				}
			}
			n++;
		}
		is.close();
	}

	private String getCellValue(Cell cell) {  
        String ret;  
        switch (cell.getCellType()) {  
        case Cell.CELL_TYPE_BLANK:  
            ret = "";  
            break;  
        case Cell.CELL_TYPE_BOOLEAN:  
            ret = String.valueOf(cell.getBooleanCellValue());  
            break;  
        case Cell.CELL_TYPE_ERROR:  
            ret = null;  
            break;  
        case Cell.CELL_TYPE_FORMULA:  
            Workbook wb = cell.getSheet().getWorkbook();  
            CreationHelper crateHelper = wb.getCreationHelper();  
            FormulaEvaluator evaluator = crateHelper.createFormulaEvaluator();  
            ret = getCellValue(evaluator.evaluateInCell(cell));  
            break;  
        case Cell.CELL_TYPE_NUMERIC:  
            if (DateUtil.isCellDateFormatted(cell)) {   
                Date theDate = cell.getDateCellValue();  
                SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
                ret = sdf.format(theDate);  
            } else {   
                ret = NumberToTextConverter.toText(cell.getNumericCellValue());  
            }  
            break;  
        case Cell.CELL_TYPE_STRING:  
            ret = cell.getRichStringCellValue().getString();  
            break;  
        default:  
            ret = null;  
        }  
        return ret;
    }  
}  
